package com.spzx.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spzx.common.core.utils.bean.BeanUtils;
import com.spzx.product.domain.Category;
import com.spzx.product.mapper.CategoryMapper;
import com.spzx.product.service.ICategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.product.vo.CategoryVo;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品分类 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2024-12-20
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {

    @Override
    public List treeSelect(Long parentId) {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<Category>();
        queryWrapper.eq(Category::getParentId, parentId);
        List<Category> categoryList = baseMapper.selectList(queryWrapper);

        if(!CollectionUtils.isEmpty(categoryList)){
            categoryList.forEach(category -> {
                //查找当前节点是否有子节点
                Long count = baseMapper.selectCount(
                        new LambdaQueryWrapper<Category>()
                                .eq(Category::getParentId, category.getId())
                );
                category.setHasChildren(count > 0);
            });
        }
        return categoryList;
    }

    @Override
    public List<CategoryVo> getLevelOneCategory() {
        //查询所有一级分类
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getParentId, 0L);
        List<Category> allCategoryList = baseMapper.selectList(queryWrapper);
        //转换为vo
        return allCategoryList.stream().map(item -> {
            CategoryVo categoryVo = new CategoryVo();
            BeanUtils.copyProperties(item, categoryVo);
            return categoryVo;
        }).collect(Collectors.toList());
    }

    @Override
    public List<CategoryVo> tree() {
        //一次拿出所有所有分类信息
        List<Category> allCategoryList = baseMapper.selectList(null);
        //转换为vo
        List<CategoryVo> categoryVoList = allCategoryList.stream().map(item -> {
            CategoryVo categoryVo = new CategoryVo();
            BeanUtils.copyProperties(item, categoryVo);
            return categoryVo;
        }).collect(Collectors.toList());

        //转为树形结构
        return buildTree(categoryVoList);
    }

    private List<CategoryVo> buildTree(List<CategoryVo> categoryVoList) {
        ArrayList<CategoryVo> treeList = new ArrayList<>();
        for (CategoryVo categoryVo : categoryVoList) {
            if (categoryVo.getParentId().longValue() == 0) {
                categoryVo = findAndAddChildren(categoryVo, categoryVoList);
                treeList.add(categoryVo);
            }
        }
        return treeList;
    }

    private CategoryVo findAndAddChildren(CategoryVo categoryVo, List<CategoryVo> categoryVoList) {
        for (CategoryVo vo : categoryVoList) {
            if (vo.getParentId().longValue() == categoryVo.getId().longValue()) {
                CategoryVo children = findAndAddChildren(vo, categoryVoList);
                categoryVo.getChildren().add(children);
            }
        }
        return categoryVo;
    }
}
